﻿﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace DesignPattern.reflect {
    public class TestReflect {
        public void Test(){
            
            
//            Type t = Type.GetType("System.Int32[]");
//            int[] array = new int[10];//初始化数组长度为10
//            array = (int[])(t.InvokeMember("Set", BindingFlags.CreateInstance,null, array, new object[] { 5 }));//这里将它的长度变为5看看是否能成功
//            Console.WriteLine(array.Length);

//            可以看到，输出结果为5，这证明，的确是用Set方法来初始化数组的。
//            接下来，我们就会想，如何为数组的元素赋值及如何获取它们的值呢？
//            再次看一下上面的方法列表，可以看到有GetValue与SetValue方法，那我们来试试：
//            复制代码 代码如下:

            Type t = Type.GetType("System.Int32[]");
            object array = new object();
            array = t.InvokeMember("Set", BindingFlags.CreateInstance, null, array, new object[] { 10 });
            for (int i = 0; i < 10; i++)
                t.GetMethod("SetValue", new Type[2] { typeof(object), typeof(int) }).Invoke(array, new object[] { i, i });
            for (int i = 0; i < 10; i++)
                Console.WriteLine(t.GetMethod("GetValue", new Type[] { typeof(int) }).Invoke(array, new object[] { i }));
            
            
            
//            Console.WriteLine("TestReflect Test start");
            ModelA modelA = new ModelA();
//            modelA._name = new []{"lishuai","333","aaa","bbb","fff","888"};
//            modelA._path = "asset/cha";
            ModelA.name = new[]{"ddd", "zzz"};
//
//            ModelA.list=new List<string>{
//                "aaa","bbb","fff","888"
//            };
//            
            var infos = modelA.GetType().GetProperties();
            for (int i = 0; i < infos.Length; i++){
                Console.WriteLine(i + "," + infos[i].Name + "," + infos[i].GetGetMethod().Invoke(modelA, null));
                if (infos[i].PropertyType.IsArray){
                    
                    Console.WriteLine("\t师叔祖"+infos[i].PropertyType.GetArrayRank());
                    Console.WriteLine("\t师叔祖"+infos[i].GetAccessors().Length);
                    Console.WriteLine("\t师叔祖"+infos[i].GetAccessors()[0].Invoke(modelA,null));
                    Console.WriteLine("\t师叔祖"+infos[i].GetAccessors()[1].Invoke(modelA,new[]{"aaa","12345","aaa","12345","aaa","12345",}));
                }
                else if (infos[i].PropertyType.IsGenericType){
                    Console.WriteLine("\t是集合"+infos[i].PropertyType.GetGenericArguments());    
                }
                
            }

//            ModelB modelB=new ModelB();
//            modelB._a = "aaaa";
//            modelB._b = 1231;

//            Init(modelA);
//            Init(modelB);
//            Console.WriteLine(modelA._name);
            Console.WriteLine(ModelA.name);
//            Console.WriteLine(modelA._path);
//            Console.WriteLine(ModelA.path);

//            Console.WriteLine(modelB._a);
//            Console.WriteLine(ModelB.a);
//            Console.WriteLine(modelB._b);
//            Console.WriteLine(ModelB.b);
        }

//        public void Init(Object obj){
//            //反射赋值
//            PropertyInfo[] prop = obj.GetType().GetProperties();
//            if (prop.Length % 2 != 0) return;
//            for (var i = 0; i < prop.Length; i++, i++){
//                prop[i].GetSetMethod().Invoke(obj, new[]{prop[i + 1].GetGetMethod().Invoke(obj, null)});
//            }
//        }

        public void Init<T>(T obj){
            PropertyInfo[] infos = obj.GetType().GetProperties();
            if (infos.Length % 2 != 0) return;
            for (var i = 0; i < infos.Length; i++, i++){
                infos[i].GetSetMethod().Invoke(obj, new[]{infos[i + 1].GetGetMethod().Invoke(obj, null)});
            }
        }
        
        
        
        
    }
}